var GameData = require('../module/gameData')();

cc.Class({
    extends: cc.Component,

    properties: {
        sp1: cc.Sprite,
        contet: {
            default: [],
            type: cc.Node
        },

        coinNum: cc.Label,
        // cost: cc.Label,
        // title: cc.Label,
        // nownum: cc.Label,
        // addnum: cc.Label,
        bullet1: cc.Label,
        bullet2: cc.Label,

        btn1: cc.Sprite,


    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.fishData = GameData.getlocalData("fishData");
        this.coinNum.string = this.fishData.coinNum + "";
        this.bullet1.string = GameData.gameText.up_zidan.dsc1;
        this.bullet2.string = GameData.gameText.up_zidan.dsc2;
        this.updateBtn();
        for (var a = 0; a < GameData.gameText.up_zidan.shuxing.length; a++) {
            // this.contet[a].getChildByName("title1").getComponent(cc.Label).string = GameData.gameText.up_zidan.shuxing[a];
            this.updateLevel(a);
            this.updateCost(a);
            this.updateAddNum(a);
        }

    },

    selectBtn(evt) {
        let _name = evt.currentTarget.name;
        var that = this;
        switch (_name) {
            case "btn1":
                that.upgrade(0);
                break;
            case "btn2":
                that.upgrade(1);
                break;
            case "btn3":
                that.upgrade(2);
                break;
            case "btn4":
                that.upgrade(3);
                break;
            case "btn5":
                that.upgrade(4);
                break;


        }
    },
    upgrade(idx) {
        this.fishData = GameData.getlocalData("fishData");
        var _level = this.fishData.upgrade.up_zidan[idx];

        if (_level <= 9) {
            var _costNum = GameData.upgradeData.zidan[_level].price;
            if (this.fishData.coinNum >= _costNum) {
                this.coinNum.node.parent.getComponent("gamecoin").coinAddEffect(this.fishData.coinNum, this.fishData.coinNum - _costNum);

                _level++;
                this.coinNum.node.parent.getComponent("gamecoin").coinAddEffect(this.fishData.coinNum, this.fishData.coinNum - _costNum);
                this.fishData.coinNum -= _costNum;
                this.fishData.upgrade.up_zidan[idx] = _level;

                GameData.savelocalData(this.fishData);
                this.updateLevel(idx);
                this.updateCost(idx);
                this.updateAddNum(idx);
            }
        } else {

        }
        this.updateBtn();
    },

    updateLevel(idx) {
        for (var a = 0; a < this.fishData.upgrade.up_zidan[idx]; a++) {
            var obj = this.contet[idx].getChildByName("jindutiao").getChildByName("content1");
            obj.getChildByName("sp" + (a + 1)).getComponent(cc.Sprite).spriteFrame = this.sp1.getComponent(cc.Sprite).spriteFrame;
        }
    },
    updateCost(idx) {
        var _level = this.fishData.upgrade.up_zidan[idx];
        var _costNum1;
        if (_level <= 9) {
            _costNum1 = GameData.upgradeData.zidan[_level].price;
        } else {
            _costNum1 = "MAX";
        }
        this.contet[idx].getChildByName("btn" + (idx + 1)).getChildByName("costNum").getComponent(cc.Label).string = _costNum1;

    },
    updateAddNum(idx) {
        var _level = this.fishData.upgrade.up_zidan[idx];
        var _addnum;
        var _nownum;
        var _Arr = ["attack", "g_speed", "f_speed", "rope_len", "bullet_num"];
        var _str = _Arr[idx];
        if (_level <= 9) {
            var _level1 = _level - 1;
            var _addnum = GameData.upgradeData.zidan[_level][_str] - GameData.upgradeData.zidan[_level1][_str];
            _addnum = _addnum.toFixed(1)
            this.contet[idx].getChildByName("addNum").getComponent(cc.Label).string =  "+"+ _addnum + "";
            this.contet[idx].getChildByName("nowNum").getComponent(cc.Label).string = GameData.upgradeData.zidan[idx][_str] + "";
        } else {
            _addnum = "0";
            this.contet[idx].getChildByName("addNum").getComponent(cc.Label).string =  "+"+ _addnum + "";
            this.contet[idx].getChildByName("nowNum").getComponent(cc.Label).string = GameData.upgradeData.zidan[9][_str] + "";
        }
    },
    updateBtn(){
        for(var idx=0;idx<5;idx++){
            var _level = this.fishData.upgrade.up_zidan[idx];
            var _costNum1;
            if (_level <= 9) {
                _costNum1 = GameData.upgradeData.zidan[_level].price;
                if (this.fishData.coinNum <_costNum1) {
                    this.contet[idx].getChildByName("btn" + (idx + 1)).getComponent(cc.Sprite).spriteFrame=this.btn1.getComponent(cc.Sprite).spriteFrame;
                }
            }
        }

    },
    start() {

    },

    // update (dt) {},
});
